<template>
    <view v-if="!loading">
        <st-uni-dynamic-form :form-items="tagForm" @submit-form="submitForm"></st-uni-dynamic-form>
    </view>
</template>

<script>
    import StUniDynamicForm from '../../components/StUniDynamicForm'
    import {mapState} from 'vuex'

    export default {
        computed: mapState([
            'userInfo',
        ]),
        components: {StUniDynamicForm},
        data() {
            return {
                loading: true,
                tagForm: [
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'natureTypeParent',
                        label: '性格',
                        multiple: true,
                        max: 5,
                        type: 'nature_type_parent'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'hobbyTypeParent',
                        label: '爱好',
                        multiple: true,
                        max: 5,
                        type: 'hobby_type_parent'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'foodTypeParent',
                        label: '美食',
                        multiple: true,
                        max: 5,
                        type: 'food_type_parent'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'movieTypeParent',
                        label: '电影',
                        multiple: true,
                        max: 5,
                        type: 'movie_type_parent'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'tripTypeParent',
                        label: '旅行',
                        multiple: true,
                        max: 5,
                        type: 'trip_type_parent'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'sportTypeParent',
                        label: '运动',
                        multiple: true,
                        max: 5,
                        type: 'sport_type_parent'
                    }
                ]
            }
        },
        created() {
            this.getDetail()
        },
        methods: {
            submitForm(data) {
                let formData = {}
                formData.userId = this.userInfo.id
                let tags = []

                for (let key in data) {
                    let tag = {userId: formData.userId, content: data[key].join(',')}
                    this.tagForm.forEach(item => {
                        if (item.name === key) {
                            tag.typeDictId = item.type
                            return
                        }
                    })
                    tags.push(tag)
                }
                formData.tags = tags

                this.$http.post('/app/user-tag-edit', formData).then(function (res) {
                    uni.showModal({
                        title: '提示',
                        confirmText: '返回',
                        content: '保存成功！',
                        success: res => {
                            if (res.confirm) {
                                uni.switchTab({
                                    url: '/pages/user/user'
                                })
                            }
                        }
                    })
                }).catch(function (res) {
                    uni.showToast({
                        icon: 'none',
                        title: res.data.errorMsg
                    });
                })
            },
          getDetail() {
              uni.showLoading({
                  title: '加载中'
              })

              this.$http.get('/user-tag/getList?userId=' + this.userInfo.id).then(res =>{
                let form = []
                Object.assign(form, this.tagForm)

                form.forEach(e => {
                  res.data.forEach(tag => {
                    if (e.type === tag.typeDictCode) {
                      e.value = tag.content.split(',')
                      return
                    }
                  })
                })
                this.tagForm = form
              }).catch(error=>{

              }).finally(()=>{
                uni.hideLoading()
                this.loading = false
              })
            }
        }
    }
</script>

<style>

</style>
